// 
import {tree,utils} from "../../packages"
import store from "@/store/index"
import router from "@/router/index"
// import {NodeIntf} from "./router.config"
// import {RouteRecordNormalized} from "vue-router"

/* 
{id:'@id',type:'menu',icon:'',label:'',path:'',target:'',name:'',children:[]}
{id:'@id',type:'route',name:'',path:'',redirect:'',component:'',meta:{},children:[]}
*/

/* ===== 系统菜单组件配置 == */
export const menuOptions = {
  mode:'horizontal' ,//  水平还是垂直
  // backgroundColor:'#aaa',
  collapse:false,
  textColor:'#fff',
  activeTextColor:'#FF6F4B',
  menuTrigger:'click' ,// click | hover
  router:true, // 
  collapseTransition:true, // 
}
/* ====================== */


export interface MenuItem {
  type:string;
  enabled?:boolean;
  name?:string;
  children?:MenuItem[];
  path?:string;
  //
  tag?:string;
  icon?:string;
  label?:string;
  target?:string;
  //
  redirect?:string;
  component?:string;
  meta?:any;
  alias?:string;
  //

}
/* 解析菜单数据 */

// 提取所有数据
export const getAllPath = (source:MenuItem[]):MenuItem[][] => {
  return tree.paths(source)
}

// 过滤掉无效数据 也可以后台过滤
export const filterPath = (paths:MenuItem[][]) => {
  return paths.filter((items:MenuItem[]) => items[0].enabled === undefined || items[0].enabled)
}
// 提取路由数据
export const saveRoutes = (source:MenuItem[]) => {
  const ts = ['menu','route']
  const keys = 'path,name,redirect,component,meta,alias'.split(',')
  const fn:tree.CallbackAny<MenuItem> = (n:any) => {
    if(n.enabled === false){return false;}
    if(n.type && !ts.includes(n.type)){return false;}
    const d:any = {};
    keys.forEach(k => {
      const v:any = n[k]
      v !== undefined && (d[k] = v);
    })
    return d
    // return n;
  }
  // console.log(source)
  const routes:any = tree.reset(source,fn)

  // const tempNode:any = {};
  // const tempPath:string[] = [];
  // const routes:any = [];
  // //
  // tree.map(source,(n:any,l,i,pn,pi) => {
  //   //
  //   if(pn && pn.path){
  //     n.path = pn.path + '/' + n.path;
  //   }
  //   //
  //   const fpath = pn ? pn.path : '';
  //   if(ts.includes(n.type) && n.component){
  //     const path:string = n.path || '';
  //     const node:any = {children:[]};
  //     keys.forEach(k => {
  //       if(n[k]){
  //         node[k] = n[k];
  //       }
  //     })
  //     let fnode:any = undefined;
  //     if(fpath && tempPath.includes(fpath)){
  //       fnode = tempNode[fpath]
  //     }else{
  //       fnode = {children:routes}
  //     }
  //     tempPath.push(path)
  //     tempNode[path] = node;
  //     fnode.children.push(node);
  //   }
  // })
  console.log('routes = ',JSON.stringify(routes))
  store.commit('setRoutes',routes)
  //
  router.$rebuild && router.$rebuild();
  //
  // setTimeout(() => {
  //   console.log(router.getRoutes())
  //   // router.push('/huarun')
  // },1000)
}
// 提取菜单数据
export const saveMenus = (source:MenuItem[]) => {
  const tempNode:any = {};
  const tempPath:string[] = [];
  const menus:any = [];
  tree.map(source,(n,l,i,pn,pi) => {
    if(pn && pn.path && n.path && !utils.isURL(pn.path) && !utils.isURL(n.path)){
      n.path = pn.path + '/' + n.path;
    }
    const fpath = pn ? pn.path : '';
    const {name,path='',icon,label,target,tag} = n;
    if(n.type === 'menu'){
      let fnode:any = undefined;
      const node = {name,path,icon,label,target,tag,children:[]}
      if(fpath && tempPath.includes(fpath)){
        fnode = tempNode[fpath]
      }else{
        fnode = {children:menus}
      }
      tempPath.push(path)
      tempNode[path] = node;
      fnode.children.push(node);
    }
  })
  //
  console.log('menus = ',JSON.stringify(menus))
  //
  store.commit('setMenus',menus)
}
// 提取权限数据
export const savePermissions = (source:MenuItem[]) => {
  const ts = ['button']
  const paths = tree.leafs(source);
  const leafs = paths.filter((items:MenuItem[]) => ts.includes(items[0].type))
  const permissions:any = {};
  leafs.forEach(items => {
    const name = items[0].name
    const ps = items.map(item => item.path).filter(p => p).map((p:any) => p.replace(/^\s?\//,''));
    const key = ps.join('.');
    if(!permissions[key]){
      permissions[key] = {};
    }
    name && (permissions[key][name] = {...items[0]});
  })
  //
  console.log('permissions = ',JSON.stringify(permissions))
  //
  store.commit('setPermissions',permissions)
}